<?php
declare(strict_types=1);


namespace App\Marketing\Mapper;

use App\Marketing\Model\MarketingCashPrize;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 现金奖励Mapper类
 */
class MarketingCashPrizeMapper extends AbstractMapper
{
    /**
     * @var MarketingCashPrize
     */
    public $model;

    public function assignModel()
    {
        $this->model = MarketingCashPrize::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 入口类型
        if (isset($params['account_type']) && filled($params['account_type'])) {
            $query->where('account_type', '=', $params['account_type']);
        }

        // 币种id
        if (isset($params['currency_id']) && filled($params['currency_id'])) {
            $query->where('currency_id', '=', $params['currency_id']);
        }

        // 调用码
        if (isset($params['code']) && filled($params['code'])) {
            $query->where('code', '=', $params['code']);
        }

        // 完成0否1是
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        return $query;
    }

    public function save(array $data): mixed
    {
        $data['no'] = random_int(1000, 9999);
        return parent::save($data);
    }
}